import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/assets_tool/app_colors.dart';
import 'package:xvideos/hj_utils/api_service/mine_service.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';

import '../../../hj_utils/global.dart';
import '../../../tools_base/loading/loading_alert_widget.dart';
import '../../../tools_base/widget/group_text_filed.dart';

class AddressWriteAlert extends StatefulWidget {
  final String? id;

  const AddressWriteAlert({
    Key? key,
    this.id,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _AddressWriteAlertState();
  }

  static show(BuildContext context, String id) async {
    Get.dialog(
      AddressWriteAlert(id: id),
      useSafeArea: false,
    );
  }
}

class _AddressWriteAlertState extends State<AddressWriteAlert> {
  TextEditingController nameController = TextEditingController();
  TextEditingController phoneController = TextEditingController();
  TextEditingController addressController = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  void commitEvent() async {
    FocusScope.of(context).unfocus();
    if (nameController.text.isEmpty == true) {
      showToast("请输入姓名");
      return;
    }
    if (phoneController.text.isEmpty == true) {
      showToast("请输入电话");
      return;
    }
    if (addressController.text.isEmpty == true) {
      showToast("请输入地址");
      return;
    }
    LoadingAlertWidget.show(context);
    bool success = await MineService.integralExchangeSomething(
      widget.id ?? '',
      name: nameController.text,
      tel: phoneController.text,
      address: addressController.text,
    );
    LoadingAlertWidget.cancel(context);
    Navigator.pop(context, true);
    if (success) {
      showToast("提交成功");
    } else {}
    // setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => Get.back(),
      child: Container(
        // decoration: BoxDecoration(
        //   image: DecorationImage(
        //     fit: BoxFit.cover,
        //     image: AssetImage('tree_note_dialog_bg.webp'.squireImgPath),
        //   ),
        // ),
        child: Scaffold(
          backgroundColor: Colors.transparent,
          body: Material(
            color: Colors.black.withOpacity(0.1),
            child: Center(
              child: _buildContent(),
            ),
          ),
        ),
      ),
      // ),
    );
  }

  Widget _buildContent() {
    return GestureDetector(
      onTap: () {
        FocusScope.of(context).unfocus();
      },
      child: Container(
        padding: EdgeInsets.fromLTRB(20, 32, 20, 24),
        margin: EdgeInsets.symmetric(horizontal: 32),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(12),
          gradient: LinearGradient(colors: AppColors.dialogBgColor, begin: Alignment.topCenter, end: Alignment.bottomCenter),
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              "收货地址",
              style: TextStyle(
                color: Colors.black87,
                fontSize: 18,
                fontWeight: FontWeight.w500,
              ),
            ),
            24.sizeBoxH,
            Container(
              height: 44,
              padding: EdgeInsets.symmetric(horizontal: 12),
              alignment: Alignment.centerLeft,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(6),
                color: Colors.black.withOpacity(0.05),
              ),
              child: _buildInput(nameController, '请输入姓名'),
            ),
            const SizedBox(height: 12),
            Container(
              height: 44,
              padding: EdgeInsets.symmetric(horizontal: 12),
              alignment: Alignment.centerLeft,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(6),
                color: Colors.black.withOpacity(0.05),
              ),
              child: _buildInput(phoneController, '请输入电话'),
            ),
            12.sizeBoxH,
            Container(
              height: 114,
              decoration: BoxDecoration(
                color: Colors.black.withOpacity(.05),
                borderRadius: BorderRadius.circular(6),
              ),
              child: TextField(
                maxLines: null,
                decoration: InputDecoration(
                  border: InputBorder.none,
                  contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 11),
                  isDense: true,
                  hintText: '请输入地址',
                  hintStyle: TextStyle(color: Colors.black.withOpacity(.3), fontSize: 14),
                  hintMaxLines: 10,
                  counter: SizedBox(),
                ),
                style: const TextStyle(
                  color: Colors.black,
                  fontSize: 14,
                  fontWeight: FontWeight.w400,
                ),
                controller: addressController,
                keyboardType: TextInputType.multiline,
                maxLength: 100,
              ),
            ),
            const SizedBox(height: 18),
            _buildButtonMenu(),
          ],
        ),
      ),
    );
  }

  Widget _buildButtonMenu() {
    return GestureDetector(
      onTap: commitEvent,
      child: Container(
        alignment: Alignment.center,
        height: 44,
        decoration: BoxDecoration(borderRadius: BorderRadius.circular(20), color: AppColors.primaryRed),
        child: const Text(
          "立即提交",
          style: TextStyle(
            color: Colors.white,
            fontSize: 14,
            fontWeight: FontWeight.w500,
          ),
        ),
      ),
    );
  }

  _buildInput(TextEditingController tec, String? hintText, {double? height}) {
    return GroupTextFiled(
      height: height,
      controller: tec,
      placeholderTextStyle: TextStyle(fontSize: 14, color: Colors.black.withOpacity(.3)),
      placeholder: hintText,
      textStyle: TextStyle(
        color: Colors.black,
        fontSize: 14,
        fontWeight: FontWeight.w400,
      ),
    );
  }
}
